package com.kaishengit.web;


import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Idea;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserIdea;
import com.kaishengit.service.CommentService;
import com.kaishengit.service.IdeaService;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.Page;

public class IdeaServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private IdeaService ideaService=new IdeaService();
	private CommentService cservice=new CommentService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String i=request.getParameter("imethod");
		if(i==null||"".equals(i)){
			toIdea(request,response);
		}else if("new".equals(i)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toAdd(request,response);
			} else if("POST".equals(httpMethod)) {
				Add(request,response);
			}
		}else if("tocomment".equals(i)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				toComment(request,response);
			} else if("POST".equals(httpMethod)) {
				Comment(request,response);
			}
		}else if("edit".equals(i)){
			String httpMethod = request.getMethod();
			if("GET".equals(httpMethod)) {
				try {
					toEdit(request,response);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else if("POST".equals(httpMethod)) {
				edit(request,response);
			}
		}else if("delete".equals(i)){
			todelete(request,response);
		}
	}
	
	
	/**
	 * 删除想法
	 * @throws IOException 
	 * */
	private void todelete(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String ideaid=request.getParameter("id");
		System.out.println("ideaid:**********"+ideaid);
		ideaService.delete(ideaid);
		response.sendRedirect("idea.jspx");
	}



	/**
	 * 修改并提交想法
	 * @throws IOException 
	 * */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ideaid=request.getParameter("ideaid");
		String title=request.getParameter("title");
		String createtime=request.getParameter("createtime");
		String desc=request.getParameter("desc");
		String projectid=request.getParameter("projectid");
		String userid=request.getParameter("userid");
		ideaService.update(ideaid,title,createtime,desc,projectid,userid);
		response.sendRedirect("idea.jspx");
	}
	
	/**
	 * 进入到修改想法页面
	 * @throws IOException 
	 * @throws ServletException 
	 * @throws ParseException 
	 * */
	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
		String ideaid=request.getParameter("id");
		/*HttpSession session= request.getSession();
		User user=(User) session.getAttribute("user");
		String projectid =(String) session.getAttribute("projectid");*/
		Idea idea=ideaService.findByIdea(ideaid);
		request.setAttribute("idea", idea);
		request.getRequestDispatcher("WEB-INF/views/edit_idea.jsp").forward(request, response);
	}

	/**
	 * 保存并发表评论
	 * @throws ServletException 
	 * */
	
	private void Comment(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
			String ideaid=request.getParameter("ideaid");
			String content=request.getParameter("content");
			HttpSession session= request.getSession();
			User user=(User) session.getAttribute("user");
			String userid=user.getId();
			cservice.saveIdea(userid,content,ideaid );
			response.sendRedirect("idea.jspx?imethod=tocomment&id="+ideaid);
	}



	/**
	 * 进入到评论的页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String ideaid=request.getParameter("id");
		UserIdea userIdea=ideaService.findByIdeaId(ideaid);
		List<Comment> list=cservice.findByideaid(ideaid);
		request.setAttribute("Commentlist", list);
		request.setAttribute("userIdea", userIdea);
		request.getRequestDispatcher("WEB-INF/views/idea_show.jsp").forward(request, response);
	}

	/**
	 * 提交想法
	 * @throws IOException 
	 * */
	
	private void Add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String title=request.getParameter("title");
		String content=request.getParameter("desc");
		HttpSession session= request.getSession();
		User user=(User) session.getAttribute("user");
		String projectid =(String) session.getAttribute("projectid");
		String userid=user.getId();
		ideaService.save(title,content,userid,projectid);
		response.sendRedirect("idea.jspx");
	}
	

	/**
	 * 进入新建想法页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */

	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/new_idea.jsp").forward(request, response);
	}



	/**
	 * 进入到想法表idea.jsp页面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	private void toIdea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String p = request.getParameter("p");
		int pageNum =0;
		if(p == null || "".equals(p)){
			pageNum = 1;
		}else{
			pageNum = Integer.valueOf(p);
		}
		
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		String nowtime=DateUtil.getNow();
		Page<UserIdea> userIdealist=ideaService.findAllByProjectId(pageNum,projectid,nowtime);
		User user=(User) session.getAttribute("user");
		String userid=user.getId();
		request.setAttribute("userid", userid);
		request.setAttribute("userIdeaList", userIdealist);
		request.getRequestDispatcher("WEB-INF/views/idea.jsp").forward(request, response);
	}
}
